import { useAccountStore } from '@/store/account'
import _ from 'lodash'

export default {
  mounted(el, binding, vnode) {
    const { value } = binding
    if (!_.isString(value)) {
      throw new Error(`操作权限标签值必须为字符串`)
    }
    const accountStore = useAccountStore()
    const permissions = accountStore.permissions
    const hasPermission =
      isAdmin(permissions) || _.indexOf(permissions, value) !== -1
    // console.log(hasPermission, value)
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

const all_permission = '*'

function isAdmin(permissions) {
  return (
    permissions.length &&
    permissions.length === 1 &&
    permissions[0] === all_permission
  )
}
